//Page Object
import { getSetting, openSetting, chooseAddress, showModal, showToast } from "../../utils/asyncWx.js";

Page({
  data: {
    address: {},
    cart: [],
    Allchecked: false
  },
  //options(Object)
  onLoad: function (options) { },
  onReady: function () { },
  onShow: function () {
    const address = wx.getStorageSync("address");
    const cart = wx.getStorageSync("cart") || [];
    let Allchecked = cart.length ? cart.every(v => v.checked) : false;
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(v => {
      if (v.checked) {
        totalPrice += v.num * v.goods_price;
        totalNum += v.num;
      } else {
        Allchecked = false;
      }
    });

    this.setData({
      address,
      cart,
      Allchecked,
      totalNum,
      totalPrice
    });
  },
  onHide: function () { },
  async handleChooseAddress() {
    try {
      const res1 = await getSetting();
      const scopeAddress = res1.authSetting["scope.address"];
      if (scopeAddress === false) {
        await openSetting();
      }
      const res2 = await chooseAddress();
      wx.setStorageSync("address", res2);
    } catch (error) {
      console.error(error);
    }
  },
  handleItemChange(e) {
    const index = e.currentTarget.dataset.index;
    let { cart } = this.data;
    cart[index].checked = !cart[index].checked;
    this.setCart(cart);
  },
  //全选
  handleItemAllCheck() {
    let { cart, Allchecked } = this.data;
    Allchecked = !Allchecked;
    cart.forEach(v => (v.checked = Allchecked));
    this.setCart(cart);
  },

  //商品增减
  async handleItemEdit(e) {

    const { operation, id } = e.currentTarget.dataset
    let { cart } = this.data
    const index = cart.findIndex(v => v.goods_id === id)
    if (cart[index].num === 1 && operation === -1) {

      const res = await showModal({ content: '是否确定删除该商品' })
      if (res.confirm) {
        cart.splice(index, 1)
        this.setCart(cart)
      }
    } else {
      cart[index].num += operation
      this.setCart(cart)
    }


  },
  //购物车结算
  async handlePay() {
    let { address, cart } = this.data
    if (cart.length === 0) {
      await showToast({ title: '请先添加商品' })
    } else if (address === '') {
      await showToast({ title: '请选择收货地址' })
    } else {
      wx.navigateTo({
        url: '/pages/pay/pay',
        sucess: (result) => {
          console.log(result);
        },
        fail: () => {
        },
        complete: () => {

        }
      })
    }
  },


  setCart(cart) {
    let Allchecked = cart.length ? cart.every(v => v.checked) : false;
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(v => {
      if (v.checked) {
        totalPrice += v.num * v.goods_price;
        totalNum += v.num;
      } else {
        Allchecked = false;
      }
    });
    this.setData({
      cart,
      Allchecked,
      totalPrice,
      totalNum
    });
    wx.setStorageSync("cart", cart);
  },
});
